import { CameraHelper } from "three";
import { axes } from "./objects/axes";
import { controls } from "./objects/controls";
import { mainCamera } from "./objects/main.camera";
import { mainScene } from "./objects/main.scene";
import { person } from "./objects/meshs/girl.mesh";
import { worldBg } from "./objects/meshs/worldBg.mesh";
import { mainPlane } from "./objects/plane";
import { renderer } from "./objects/renderer";
import { sunlight } from "./objects/sunlight";
import { initStyle } from "./tools/initSystem.tool";
import { setGUI } from "./tools/gui.tool";
import { qino } from "./objects/meshs/qino.mesh";
import { cube } from "./objects/meshs/cube.mesh";
import { animationPool } from "./animationPool";
import { gamepad } from "./gamepad/gamepad.tool";
import { setStats } from "./tools/stats.tool";

if (navigator.gpu) {
  console.log('你的浏览器支持WebGPU。那又怎样！');
} else {
  console.log('你的浏览器不支持WebGPU，请更换新版本浏览器。');
}


async function init3DObject() {
  mainCamera.init();
  controls.init();
  sunlight.init();
  axes.init();
  await Promise.allSettled([
    await mainPlane.loaded,
    await worldBg.loaded,
    await person.loaded,
    await qino.loaded,
  ]);
  addToScene();
}

function addToScene() {
  mainScene.add(axes.mesh);
  mainScene.add(mainCamera.mesh);
  mainScene.add(mainPlane.mesh);
  mainScene.add(sunlight.mesh);
  // mainScene.add(new CameraHelper(sunlight.mesh.shadow.camera));
  mainScene.add(worldBg.mesh);
  mainScene.add(person.mesh);
  mainScene.add(cube.mesh);
  mainScene.add(qino.mesh);
}

initStyle();
init3DObject();
setGUI();
setStats();
gamepad.init();

let animationPoolSize = 0;
function StartAnimation() {
  if (animationPoolSize !== animationPool.size) {
    animationPoolSize = animationPool.size;
  }
  animationPool.forEach(func => func());

  renderer.render(mainScene, mainCamera.mesh);
  window.requestAnimationFrame(StartAnimation);
}
StartAnimation();
